import { Component, OnInit } from '@angular/core';
import { UserGroupsService} from './user-groups.service';
import { Response} from '@angular/http';
import {GlobalService} from "../../global.service";


@Component({
  selector: 'app-user-groups',
  templateUrl: './user-groups.component.html',
  styleUrls: ['./user-groups.component.css']
})
export class UserGroupsComponent implements OnInit {

  data:UserGroupsInfo[] = [];
  queryData:UserGroupsInfo = new UserGroupsInfo;

  constructor(
    private globalService:GlobalService,
    private userGroupsService:UserGroupsService,
  ) { }

  ngOnInit() {

    let user = this.globalService.getCurrentUser();
    console.log("user Data :",user);
    this.queryData.properties.creator = user.code;
    this.query();
  }

  query(){
    let _this = this;
    this.userGroupsService.findUserGroupInfos(this.queryData).subscribe((res:Response)=>{
      _this.data = [];
        console.log(res.json());
        res.json().rows.forEach(function (item) {
          _this.data.push({
            code:item.code,
            name:item.name,
            remark:item.remark,
            creator:item.creator,
            userNumber:item.users.length,
            properties:new Properties,
          });
        });
        console.log(_this.data);

    });
  }

  delete(event){
    let code = event.target.name;
    this.userGroupsService.deleteGroups({code:code}).subscribe((res:Response)=>{
      this.query();
    });
  }

}
class UserGroupsInfo{
  code:string;
  name:string;
  remark:string;
  creator:string;
  userNumber:number;
  properties:Properties = new Properties;
}
class Properties {
  name_like:any;
  creator:any;
}
